package com.zhulong.saas.cloud.gateway.config;

import com.zhulong.saas.cloud.gateway.adapt.UserAdaptImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Configuration
public class CacheConfig {

/*    @Bean
    public CacheManager cacheManager() {

        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        return caffeineCacheManager;
//        RedisCacheManager redisCacheManager = new RedisCacheManager();

    }*/

    @Component
    public static class RedisCacheCustomizer implements RedisCacheManagerBuilderCustomizer {

        @Value("${spring.cache.redis.key-prefix}")
        private String cacheKeyPref;

        @Override
        public void customize(RedisCacheManager.RedisCacheManagerBuilder builder) {
            RedisCacheConfiguration defaultConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                    .prefixCacheNameWith(cacheKeyPref);
            builder.withCacheConfiguration(UserAdaptImpl.SUBJECT_BUSTYPE_CACHE_NAME, defaultConfiguration.entryTtl(Duration.ofHours(1L)));
            builder.withCacheConfiguration(UserAdaptImpl.SUBJECT_TENANT_CACHE_NAME, defaultConfiguration.entryTtl(Duration.ofHours(12L)));
            builder.withCacheConfiguration(UserAdaptImpl.HOST_TENANT_CACHE_NAME, defaultConfiguration.entryTtl(Duration.ofDays(1L)));
        }
    }

}
